(function () {
    'use strict';

    angular.module('app.ui.form.validation')
        .controller('formConstraintsCtrl', ['$scope', formConstraintsCtrl])
        .controller('signinCtrl', ['$scope','logger','$http','$location', signinCtrl])
        .controller('signupCtrl', ['$scope', signupCtrl]);

    function formConstraintsCtrl($scope) {
        var original;

        $scope.form = {
            required: '',
            minlength: '',
            maxlength: '',
            length_rage: '',
            type_something: '',
            confirm_type: '',
            foo: '',
            email: '',
            url: '',
            num: '',
            minVal: '',
            maxVal: '',
            valRange: '',
            pattern: ''
        };

        original = angular.copy($scope.form);

        $scope.revert = function() {
            $scope.form = angular.copy(original);
            return $scope.form_constraints.$setPristine();
        };

        $scope.canRevert = function() {
            return !angular.equals($scope.form, original) || !$scope.form_constraints.$pristine;
        };

        $scope.canSubmit = function() {
            return $scope.form_constraints.$valid && !angular.equals($scope.form, original);
        };
    }

    function signinCtrl($scope,logger,$http,$location) {

        $scope.user = {
            email: 'admin@qq.com',
            username: '', 
            password: ''
        };

        $scope.canSubmit = function() {
            return $scope.verifyPassword($scope.user.password) && $scope.verifyEmail($scope.user.username);
        };

        $scope.submitForm = function() {
            if(!$scope.canSubmit()){
                logger.logError("用户名或密码输入错误");
                return;
            }

            $scope.login({username:$scope.user.username,password:$scope.user.password},function(data){
                if(data && data.data){
                    var loginData = data.data;
                    if(loginData.result){
                        logger.logSuccess("登陆成功");
                        LOCAL_STORE.context.open(GLOBAL_SESSION);
                        LOCAL_STORE.context.session.user = loginData.user;
                        LOCAL_STORE.context.rme(GLOBAL_SESSION);

                        $location.path('/');
                    }
                    else{
                        logger.logError(JSON.stringify(loginData.message));
                    }

                }else {
                    logger.logError("网络访问异常异常!");
                }

            },function(err){
                logger.logError("请求服务器失败!");
            });

        };

        $scope.verifyEmail = function (email)
        {
            var emailReg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
            return emailReg.test(email);
        };
        $scope.verifyPassword = function(password){
            if(password &&  typeof(password) == 'string' && password.length >= 6){
                return true;
            }
            return false;
        }

        $scope.login = function (params, success, fail) {
            $http({
                method: 'POST',
                url: '/admin/login/',
                data: params
            }).then(success, fail);
        }

    }

    function signupCtrl($scope) {
        var original;

        $scope.user = {
            name: '',
            email: '',
            password: '',
            confirmPassword: '',
            age: ''
        };

        $scope.showInfoOnSubmit = false;

        original = angular.copy($scope.user);

        $scope.revert = function() {
            $scope.user = angular.copy(original);
            $scope.form_signup.$setPristine();
            return $scope.form_signup.confirmPassword.$setPristine();
        };

        $scope.canRevert = function() {
            return !angular.equals($scope.user, original) || !$scope.form_signup.$pristine;
        };

        $scope.canSubmit = function() {
            return $scope.form_signup.$valid && !angular.equals($scope.user, original);
        };

        $scope.submitForm = function() {
            $scope.showInfoOnSubmit = true;
            return $scope.revert();
        };

    }

})(); 